//
//  CustomAlertWindow.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/2/17.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "CustomAlertWindow.h"

UIWindow *_window;
// 窗口的高度
#define XWWindowHeight 20
// 动画的执行时间
#define XWDuration 1
// 窗口的停留时间
#define XWDelay 1.5
// 字体大小
#define XWFont [UIFont systemFontOfSize:12]

@implementation CustomAlertWindow

+ (void)showMessage:(NSString *)msg image:(UIImage *)image
{
    
    if (_window) return;
    
    // 创建按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:msg forState:UIControlStateNormal];
    [btn setImage:image forState:UIControlStateNormal];
    // 设置按钮文字大小
    btn.titleLabel.font = XWFont;
    
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    // 切掉文字左边的 10
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [btn addTarget:self action:@selector(didClickNotificationButton:) forControlEvents:UIControlEventTouchUpInside];
    // 设置数据
    
    
    
    
    // 创建窗口
    _window = [[UIWindow alloc] init];
    // 窗口背景
    _window.backgroundColor = [UIColor blackColor];
    _window.windowLevel = UIWindowLevelAlert;
    _window.frame = CGRectMake(0, -XWWindowHeight, [UIScreen mainScreen].bounds.size.width, XWWindowHeight);
    btn.frame = _window.bounds;
    [_window addSubview:btn];
    _window.alpha = 0.8;
    _window.hidden = NO;
    [_window makeKeyAndVisible];
    
    
    // 状态栏 也是一个window
    // UIWindowLevelAlert > UIWindowLevelStatusBar > UIWindowLevelNormal
    
    // 动画
    [UIView animateWithDuration:XWDuration animations:^{
        CGRect frame = _window.frame;
        frame.origin.y = 0;
        _window.frame = frame;
        NSLog(@"%@",NSStringFromCGRect(btn.frame));
//        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 20)];
//        [button addTarget:self action:@selector(didClickNotificationButton:) forControlEvents:UIControlEventTouchUpInside];
//        [_window addSubview:button];
    }completion:^(BOOL finished) {
 NSLog(@"%@",NSStringFromCGRect(btn.frame));
        [UIView animateWithDuration:XWDuration delay:XWDelay options:kNilOptions animations:^{
            CGRect frame = _window.frame;
            frame.origin.y = -XWWindowHeight;
            _window.frame = frame;
             NSLog(@"%@",NSStringFromCGRect(btn.frame));
        } completion:^(BOOL finished) {
            _window = nil;
             NSLog(@"%@",NSStringFromCGRect(btn.frame));
        }];
    }];
}

- (void)didClickNotificationButton:(UIButton *)button {
    NSLog(@"%s",__func__);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"%s",__func__);
}

@end
